var currentIndex;
$(document).ready(function(){
	getTheBlog();
	$("#blogerinfo").show();
});
//获取博主
function getTheBlog(){
	id = window.location.href.split("?")[1];
	authorId = id;
	var url = "/HyperIT/userdetail/getUserInfo";
	var parm = "userDetail_id="+id;
	$.post(url,parm,function(data){
		if (data.serviceResult) {
			var userdetail = data.resultParm.userDetail;
			initTheBlog(userdetail);
			selectCard();
		} else{
			messageTip(data.resultInfo);
		}
	});
}
function selectCard(){
	$(".navigator li").click(function(){
		$(this).addClass("active").siblings().removeClass("active");
		$("#showcontent .select-card").eq($(this).index()).show().siblings().hide();
		clickCard($(this).index());//切换选择卡，右边内容栏初始化
	});
}
function clickCard(index){
	switch (index){
		case 0:
			showBlogerInfo();
			break;
		case 1:
			initArticle();
			break;
		case 2:
			initaq();
			break;
		case 3:
			initNote();
			break;
		case 4:
			initLeaveWord();
			break;
		default:
			break;
	}
}
//初始化博主信息
function initTheBlog(userdetail){
	$("#blogName").text(userdetail.name+" 的博客空间");
	$("#blogerhead img").attr("src","../.."+userdetail.head);
	$("#blogersinatrue").text(userdetail.signature);
	$("#info_blogerhead img").attr("src","../.."+userdetail.head);
	$("#nick").text(userdetail.name);
	$("#level").text("Lv."+userdetail.level);
	var sex = "";
	if(userdetail.sex){
		sex = "女";
	}else{
		sex = "男";
	}
	$("#sex").text(sex);
	$("#ts").text(userdetail.total_sign+"天");
	$("#fans").text(userdetail.fans+"人");
	$("#artc").text(userdetail.article+"篇");
	$("#accc").text(userdetail.accept+"次");
	$("#notec").text(userdetail.note+"篇");
	$("#city").text(userdetail.city);
	$("#position").text(userdetail.position_name);
	$("#signature p").text(userdetail.signature);
	if(userdetail.skill==""||userdetail.skill==null){
		$("#skill").text("");
	}else{
		var skill = userdetail.skill.split(" ");
		for(var i = 0; i<skill.length; i++){
			$("#skill").append('<span>'+skill[i]+'</span>');
		}
	}
	if(userdetail.hobby==""||userdetail.hobby==null){
		$("#hobby").text("");
	}else{
		var hobby = userdetail.hobby.split(" ");
		for(var j = 0; j<hobby.length; j++){
			$("#hobby").append('<span>'+hobby[j]+'</span>');
		}
	}
	
	
	isFollow();
}
//切换博主资料选择卡不刷新，只显示
function showBlogerInfo(){
	$("#blogerinfo").show();
}
//获取博主的文章
function getArticle(index){
	var url = "/HyperIT/article/getPersonArticle";
	var parm = "size=10&indexPageNum="+index+"&status=1&id="+id;
	$.get(url,parm,function(data){
		if(data.serviceResult){
			var articleList = data.resultParm.article_list;
			showArticle(articleList);
			currentIndex++;
		}else{
			messageTip(data.resultInfo);
		}
	});
}
//绘制
function showArticle(articlelist){
	for(var i = 0; i<articlelist.length; i++){
		if($("#articlelist dl").length<=0||articlelist[i].createtime.substr(0,7)!=$("#articlelist>dl").last().attr("id")){
			$("#articlelist .page-bar").before('<dl id="'+articlelist[i].createtime.substr(0,7)+'">'+
							'<dt><strong>'+articlelist[i].createtime.split("-")[0]+'年'+Number(articlelist[i].createtime.split("-")[1])+'月</strong></dt>'+
							'<dd><ul>'+
							'</ul></dd>'+
						'</dl>');
		}
		$("#articlelist dl").last().find("ul").append('<li>'+
										'<a href="../article/thearticle.html?'+articlelist[i].id+'">'+
										'<h1>'+articlelist[i].title+'</h1></a>'+
										'<div class="info-bar">'+
											'<span>发布日期：'+articlelist[i].createtime.substr(0,16)+'</span>'+
											'<span>阅读量：'+articlelist[i].reading+'</span>'+
											'<span>打赏：'+articlelist[i].reward+'</span>'+
											'<span>收藏：'+articlelist[i].collecting+'</span>'+
										'</div>'+
									'</li>');
	}
}
function initArticle(){
	$("#articlelist dl").remove();
	currentIndex = 1;
	getArticle(currentIndex);
}
function getMoreArticle(){
	getArticle(currentIndex);
}
//获取问答ajax
function getaq(index){
	var url = "/HyperIT/question/getPersonQuestion";
	var parm = "userdetail_id="+id+"&size=10&indexPageNum="+index;
	$.get(url,parm,function(data){
		if (data.serviceResult) {
			var aqList = data.resultParm.question_user_list;
			showaq(aqList);
			currentIndex++;
		} else{
			messageTip(data.resultInfo);
		}
	});
}
//showaq
function showaq(aqList){
	var isAccept = "";
	for(var i = 0; i<aqList.length; i++){
		if(aqList[i].accept){
			isAccept = "已采纳";
		}else{
			isAccept = "未采纳";
		}
		$("#aqlist .page-bar").before('<dl>'+
							'<dt>'+(Number(i)+1)+'</dt>'+
							'<dd>'+
								'<a href="../aq/theaq.html?'+aqList[i].id+'"><h1>'+html_decode(aqList[i].title)+'</h1></a>'+
								'<span>提问时间：'+aqList[i].createtime.substr(0,16)+'</span>'+
								'<span>'+aqList[i].answer+'个回答</span>'+
								'<span>'+aqList[i].reading+'阅读量</span>'+
								'<span>'+isAccept+'</span>'+
							'</dd>'+
						'</dl>');
	}
}
//初始化
function initaq(){
	$("#aqlist dl").remove();
	currentIndex = 1;
	getaq(currentIndex);
}
//获取更多
function getMoreaq(){
	getaq(currentIndex);
}
//留言ajax
function saveLeaveWord(){
	if($("#leaveword").val().trim()!=""){
		var url = "/HyperIT/blog/comment";
		var parm = "userDetail_id="+thisUser.id+"&target_id="+id+"&content="+$("#leaveword").val()+"&type=2";
		$.post(url,parm,function(data){
			if(data.serviceResult){
				var comment = data.resultParm.comment;
				$("#lwlist .isay").after('<dl>'+
								'<dt><a href="../blog/theblog.html?'+comment.userDetail_id+'">'+
								'<img src="../..'+thisUser.head+'"/></a></dt>'+
								'<dd>'+
									'<p>'+comment.content+'</p>'+
									'<span>留言时间：'+comment.createtime.substr(0,16)+'</span>'+
								'</dd>'+
							'</dl>');
				$("#leaveword").val("");
				messageTip(data.resultInfo);
			}else{
				messageTip(data.resultInfo);
			}
		});
	}else{
		messageTip("哎呀！你还没有输入留言内容...");
	}
}
//获取留言
function getLeaveWord(index){
	var url = "/HyperIT/blog/getComment";
	var parm = "size=20&indexPageNum="+index+"&target_id="+id+"&type=2";
	$.get(url,parm,function(data){
		if(data.serviceResult){
			var lwlist = data.resultParm.getcomment;
			showLeaveWord(lwlist);
			currentIndex++;
		}else{
			messageTip(data.resultInfo);
		}
	});
}
//showlist
function showLeaveWord(lwlist){
	for(var i = 0;i<lwlist.length; i++){
		$("#lwlist .page-bar").before('<dl>'+
							'<dt><a href="../blog/theblog.html?'+lwlist[i].userdetail.id+'">'+
							'<img src="../..'+lwlist[i].userdetail.head+'"/></a></dt>'+
							'<dd>'+
								'<p>'+lwlist[i].comment.content+'</p>'+
								'<span>留言时间：'+lwlist[i].comment.createtime.substr(0,16)+'</span>'+
							'</dd>'+
						'</dl>');
	}
}
//init
function initLeaveWord(){
	$("#lwlist").children("dl").remove();
	currentIndex = 1;
	getLeaveWord(currentIndex);
}
//getmore
function getMoreLeaveWord(){
	getLeaveWord(currentIndex);
}
function isFollow(){
	if(getCookie("user")==null){
		return;
	}
	var url = "/HyperIT/blog/isFollow";
	var parm = "userdetail_id="+thisUser.id+"&target_id="+id;
	$.post(url,parm,function(data){
		if (data.serviceResult) {
			if(data.resultParm.follow_flag){
				$("#author-info>a").eq(0).text("取关").addClass("active").attr("onclick","cancelFollow(authorId)");
			}else{
				$("#follow").attr("onclick","follow(authorId)");
			}
		} else{
			messageTip(data.resultInfo);
		}
	});
}
//获取博主的随笔
function getNote(index){
	var url = "/HyperIT/note/findPersonNote";
	var parm = "size=10&indexPageNum="+index+"&status=0&userdetail_id="+id;
	$.get(url,parm,function(data){
		if(data.serviceResult){
			var noteList = data.resultParm.note_list;
			showNote(noteList);
			currentIndex++;
		}else{
			messageTip(data.resultInfo);
		}
	});
}
//绘制
function showNote(notelist){
	for(var i = 0; i<notelist.length; i++){
		if($("#notelist dl").length<=0||notelist[i].createtime.substr(0,7)!=$("#notelist>dl").last().attr("id")){
			$("#notelist .page-bar").before('<dl id="'+notelist[i].createtime.substr(0,7)+'">'+
							'<dt><strong>'+notelist[i].createtime.split("-")[0]+'年'+Number(notelist[i].createtime.split("-")[1])+'月</strong></dt>'+
							'<dd><ul>'+
							'</ul></dd>'+
						'</dl>');
		}
		$("#notelist dl").last().find("ul").append('<li>'+
														'<a href="../note/thenote.html?'+notelist[i].id+'">'+
														'<h1>'+notelist[i].title+'</h1></a>'+
														'<span>发布日期：'+notelist[i].createtime.substr(0,16)+'</span>'+
														'<span>收藏：'+notelist[i].collecting+'</span>'+
														'<span>阅读量：'+notelist[i].reading+'</span>'+
													'</li>');
	}
}
function initNote(){
	$("#notelist dl").remove();
	currentIndex = 1;
	getNote(currentIndex);
}
function getMoreNote(){
	getNote(currentIndex);
}